package com.ai.controller;

import com.ai.domain.po.ChatMessage;
import com.ai.service.AiRedisService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/ai")
public class ChatController {

    private final ChatClient chatClient;

    private final AiRedisService aiRedisService;
    @GetMapping( value = "/chat", produces = "text/html;charset=UTF-8")
    public Flux<String> chat(String prompt, String chatId) {
        // 发起提问
        Flux<String> content = chatClient.prompt()
                .user(prompt)// 用户的提问信息
                .advisors(advisorSpec -> advisorSpec.param(AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY, chatId))
                .stream()
                .content();//获取模型返回的答案,直接获取大模型返回的用户需要的答案

        return content;
    }

    @GetMapping("/history/{type}")
    public List<String> getHistory(@PathVariable String type) {
        return aiRedisService.GetChatIds(type);
    }

    @GetMapping("/history/{type}/{chatId}")
    public String getHistory(@PathVariable String type, @PathVariable String chatId) {
        log.info("对话的类型: {}，对话的记录的Id{}", type,chatId);

       return aiRedisService.GetChatHistory(type, chatId);

    }


}
